//
//  TLSwipeView.m
//  FunnyVideo
//
//  Created by 李鑫 on 15/10/20.
//  Copyright © 2015年 NewEggTech. All rights reserved.
//

#import "TLSwipeView.h"
#import "TLVideoPlayer.h"
#import "TLGifAniButton.h"
#import "AVOSManger.h"

#import "TLBrowseCreateManger.h"
#import <QuartzCore/QuartzCore.h>
#import "NSDate+Interval.h"
#import "TXYVideoThumbFetcher.h"


@implementation TLSwipeView

- (id)initWithCoder:(nonnull NSCoder *)aDecoder{
    self = [super initWithCoder:aDecoder];
    
    if(self)
    {
        
    }
    
    return self;
}

- (void)awakeFromNib{
    if(![[AVUser currentUser] isAuthenticated])
        [self.supportAniBtn setTriggerOnTouch:NO];
    
    CGRect targetRect = self.frame;
    
    targetRect.size = [TLDeviceManger deviceScreenSize];
    self.frame = CGRectMake(self.frame.origin.x, self.frame.origin.y, targetRect.size.width, targetRect.size.height);
    
    [self.supportAniBtn addTarget:self action:@selector(userTapSupportBtn:) forControlEvents:UIControlEventValueChanged];
    
    [self redrawViews];
    [self load];
}

- (void)redrawViews{
    self.headImg.layer.cornerRadius = 25.f;
    self.headImg.layer.borderColor = [UIColor whiteColor].CGColor;
    self.headImg.layer.borderWidth = 3.f;
    self.headImg.clipsToBounds = YES;
}


- (void)load{
    TLBrowseCreateManger *createManger= [TLBrowseCreateManger sharedManger];
    
    [createManger createRandomVideoInfo:^(TLBrowseVideoInfo *infoLoadedVideoInfo) {
        self.nameLabel.text = infoLoadedVideoInfo.name;
        self.createdAt.text = [infoLoadedVideoInfo.createdAt minuteToNowAndLargestInterval:60];
        self.descriptionLabel.text = infoLoadedVideoInfo.desc;
        self.supportLabel.text = infoLoadedVideoInfo.support;
        self.videoURL = infoLoadedVideoInfo.videoURL;
        self.v_id = infoLoadedVideoInfo.v_obj.objectId;
        self.isload = infoLoadedVideoInfo.isload;
        if(self.videoURL)
    [TXYVideoThumbFetcher fetchImageFromURL:self.videoURL completionBlock:^(UIImage *image) {
            dispatch_async(dispatch_get_main_queue(), ^{
                if(image && [image isKindOfClass:[UIImage class]])
                [self.videoThumbnail setImage:image];
            });
        }];
    } bigDataLoaded:^(TLBrowseVideoInfo *imageLoadedVideoInfo) {
        dispatch_async(dispatch_get_main_queue(), ^{
            if(imageLoadedVideoInfo.headImg)
            {
                [self.headImg setImage:imageLoadedVideoInfo.headImg];
                self.isload = imageLoadedVideoInfo.isload;
            }
        });
    }];
    
    
}

- (void)userTapSupportBtn:(id)sender{
    
}


- (IBAction)suptBtnClick:(TLGifAniButton *)sender {
    BOOL isOn = [sender on];
    NSLog(@"Support Button Click %d",isOn);
    if(isOn)
    {
        [self support];
    }
}

- (void)unsupport{
    [[AVOSManger sharedManger] cancleVideoId:self.v_id completion:^(NSUInteger sptNum, BOOL succ) {
        if(succ)
            [self.supportLabel setText:[NSString stringWithFormat:@"%ld",sptNum]];
        else
        {
            [TLAlertHelper showErrorHUDWithText:@"点赞失败"];
        }
    }];
}

- (void)support{
    [[AVOSManger sharedManger] supportVideoId:self.v_id completion:^(NSUInteger sptNum, BOOL succ) {
        if(succ)
            [self.supportLabel setText:[NSString stringWithFormat:@"%ld",sptNum]];
    }];
}

- (IBAction)playVideo:(id)sender {
    self.player.url = [NSURL URLWithString:self.videoURL];
    
    [self.player addPlayerEventCallBack:^(TLVideoPlayerEventType eventType, float value) {
        
    }];
    
    [self.player play];
}

- (void)stopPlayer{
    [self.player stop];
}
@end
